<script setup lang="ts">
import { addcomdataFn, changecommunityFn } from '@/api/communityApi';
import { useStore } from '@/stores/user';
import type { Ref } from 'vue';
import { ref } from 'vue';
import u4 from '@/assets/下载 (4).png'
import { useRouter } from 'vue-router';
import { modStore } from '@/stores/modcom';



const mod = modStore()

console.log('修改页面 查看modstore', mod);

interface iFile {
    content: string
}

const fileList: Ref<any> = ref(mod.imageList);

console.log('查看filelist初始值', fileList.value);


const afterRead = (file: any) => {
    console.log('添加/更换图片 查看file', fileList.value);
};



const store = useStore()
const date = new Date()

const message: Ref<string> = ref(mod.title || '')
const router = useRouter()
function onClickRight() {

    if (mod.modflog == false) {
        addcomdataFn({
            id: store.userid,
            time: String(date.toLocaleString()),
            title: message.value,
            img1: fileList.value[0].content,
            img2: fileList.value[1].content,
            img3: fileList.value[2].content,
            share: 0,
            chat: 0,
            photo: u4,
        }).then(res => {
            message.value = ''
            fileList.value = []
            console.log('请求发送成功---清除', message.value = '', fileList.value = []);
            router.push('/community')

        })
    } else {
        changecommunityFn({
            index:mod.index,
            id: store.userid,
            time: String(date.toLocaleString()),
            title: message.value,
            img1: fileList.value[0].content,
            img2: fileList.value[1].content,
            img3: fileList.value[2].content,
            share: mod.share,
            chat: mod.chat,
            photo: u4,
        }).then(res => {
            message.value = ''
            fileList.value = []
            console.log('请求发送成功---清除', message.value = '', fileList.value = []);
            router.push('/community')

        })
    }
        
}
</script>

<template>
    <div class="container">
        <header class="navtop">
            <van-nav-bar title="发布动态" right-text="发布" left-arrow @click-left="$router.back()" z-index="99" fixed
                :border="false" safe-area-inset-top @click-right="onClickRight" />
        </header>


        <van-cell-group inset>
            <van-field v-model="message" :autosize="{ maxHeight: 500, minHeight: 180 }" type="textarea" maxlength="150"
                placeholder="请输入内容" show-word-limit />
        </van-cell-group>

        <div class="upload">
            <van-uploader v-model="fileList" multiple :after-read="afterRead" />
        </div>
    </div>

</template>
<style scoped lang="scss">
.container {
    padding: 0 5%;
}

.navtop {
    height: 100px;

    .van-nav-bar {
        --van-nav-bar-icon-color: #1b1b1b;
        // --van-nav-bar-background: rgbr(0, 0, 0, 0) !important;
        --van-nav-bar-background: rgbr(0, 0, 0, 0);
    }
}

.van-cell-group {
    border: 0.5px solid #ccc;
    margin: 0;
    margin-bottom: 20px;

    --van-field-word-limit-line-height: 10px;
    // --van-field-text-area-min-height: 200px
}
</style>